package com.xgst.land.dialog

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.xgst.land.R
import com.xgst.land.activity.check.ItemAdapter
import com.xgst.land.bean.DialogProjectBean
import com.xgst.land.bean.IceboxItemListBean
import com.xgst.land.bean.SampleDetailBean
import com.xgst.land.bean.SampleDetailBean.DataBean.ProjectBean
import com.xgst.land.utils.backgroundAlpha
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * 方案号  项目列表
 */
class ProjectDialog(context: Context,checkId:Int = -1):PopupWindow(context), OnItemClickListener {
    private val view:View
    private var adapter: ItemAdapter
    private var onClick:(Int,String)->Unit = {name,id ->}
    init {
        view =
            LayoutInflater.from(context).inflate(R.layout.dialog_item_list_layout, null, false)
        contentView = view
        isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        isTouchable = true
        width = WindowManager.LayoutParams.MATCH_PARENT
        height = ScreenUtils.getScreenHeight()/2
        animationStyle = R.style.itemPopStyle
        adapter = ItemAdapter(checkId)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        adapter.setOnItemClickListener(this)
        recyclerView.adapter = adapter

        initData()

        setOnDismissListener {
            ActivityUtils.getTopActivity().backgroundAlpha(1.0f)
        }
    }

    private fun initData() {
        projects({
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean->
                list.add(IceboxItemListBean(bean.id,bean.protocolNumber))
            }
           adapter.setNewInstance(list)
        })
    }

    fun show(onClick:(Int,String)->Unit) {
        if(isShowing){
            return
        }
        this.onClick = onClick
        ActivityUtils.getTopActivity().backgroundAlpha(0.4f)
        showAtLocation(view, Gravity.BOTTOM,0,0)
    }

    //项目
    private fun projects(onResult:(MutableList<DialogProjectBean.DataBean>)->Unit){
        EasyHttp.post(ActivityUtils.getTopActivity() as AppCompatActivity)
            .api("other/app_project/queryAllProject.do")
            .request(object : OnHttpListener<DialogProjectBean>{
                override fun onSucceed(result: DialogProjectBean) {
                    onResult(result.data)
                }

                override fun onFail(e: Exception?) {
                    dismiss()
                    ToastUtils.showShort(e?.message)
                }
            })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as IceboxItemListBean
        if(item.disable.isEmpty()){
            onClick(item.id,item.name)
            dismiss()
        }
    }
}